﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo.Web;
using Apollo.Core;
using System.Web.Security;
using Apollo;

namespace Apollo.Website
{
    public partial class Controls_Edit_FormItemPassword : System.Web.UI.UserControl, IFormItem
    {
        public FormItem Item { get; set; }

        public object TargetObject { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.LabelPropertyName.Text = this.Item.Projection.DisplayName;

                object obj = this.Item.Projection.Apply(this.TargetObject);

                this.TextBoxPropertyValue.Enabled = (this.Item.Type != FormItemType.ReadOnly);
            }
        }

        public ActionMessage Save(object obj)
        {
            if (this.Item.Type == FormItemType.ReadOnly)
                return null;

            if (string.IsNullOrEmpty(this.TextBoxPropertyValue.Text))
                return null;

            string actualPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBoxPropertyValue.Text, "MD5");

            this.Item.Projection.SetValue(obj, actualPassword);

            return null;
        }
    }
}